package com.sokal.easycare.modules.clue.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.clue.entity.ProjectClue;
import com.sokal.easycare.modules.clue.service.ProjectClueService;

/**
 * 项目线索Controller
 * @author liliangdong
 * @version 2017-04-04
 */
@Controller
@RequestMapping(value = "${adminPath}/clue/projectClue")
public class ProjectClueController extends BaseController {

	@Autowired
	private ProjectClueService projectClueService;
	
	@ModelAttribute
	public ProjectClue get(@RequestParam(required=false) String id) {
		ProjectClue entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectClueService.get(id);
		}
		if (entity == null){
			entity = new ProjectClue();
		}
		return entity;
	}
	
	@RequiresPermissions("clue:projectClue:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectClue projectClue, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectClue> page = projectClueService.findPage(new Page<ProjectClue>(request, response), projectClue); 
		model.addAttribute("page", page);
		return "modules/clue/projectClueList";
	}

	@RequiresPermissions("clue:projectClue:view")
	@RequestMapping(value = "form")
	public String form(ProjectClue projectClue, Model model) {
		model.addAttribute("projectClue", projectClue);
		return "modules/clue/projectClueForm";
	}

	@RequiresPermissions("clue:projectClue:edit")
	@RequestMapping(value = "save")
	public String save(ProjectClue projectClue, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, projectClue)){
			return form(projectClue, model);
		}
		projectClueService.save(projectClue);
		addMessage(redirectAttributes, "保存项目线索成功");
		return "redirect:"+Global.getAdminPath()+"/clue/projectClue/?repage";
	}
	
	@RequiresPermissions("clue:projectClue:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectClue projectClue, RedirectAttributes redirectAttributes) {
		projectClueService.delete(projectClue);
		addMessage(redirectAttributes, "删除项目线索成功");
		return "redirect:"+Global.getAdminPath()+"/clue/projectClue/?repage";
	}

}